package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 老杨
 * @version 1.0
 */
@Api(tags = "套餐管理接口")
@RestController
@RequestMapping("/admin/setmeal")
public class SetmealController {
    @Autowired
    SetmealService setmealService;

    @ApiOperation(value = "分页查询接口")
    @GetMapping("/page")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO){
        PageResult page = setmealService.page(setmealPageQueryDTO);
        return Result.success(page);
    }

    @ApiOperation(value = "添加套餐接口")
    @PostMapping
    //当添加 数据时，缓存对应分类下的数据发生 改变，需要根据 新增对应 的分类id 删除旧的缓存
    @CacheEvict(cacheNames = "SetmealCache",key = "#setmealDTO.categoryId")
    public  Result addSetmeal(@RequestBody SetmealDTO setmealDTO){
        setmealService.addSetmeal(setmealDTO);
        return Result.success();
    }

    @ApiOperation(value ="修改 套餐接口")
    @PutMapping
    @CacheEvict(cacheNames = "SetmealCache",allEntries = true)
    public Result update(@RequestBody SetmealDTO setmealDTO){
        setmealService.update(setmealDTO);
        return Result.success();
    }

    @ApiOperation(value = "根据 套餐id 返回 对应信息")
    @GetMapping("/{id}")
    public Result<Setmeal> listById(@PathVariable Long id){
        Setmeal setmeal = setmealService.listById(id);
        return Result.success(setmeal);
    }

    @ApiOperation(value = "根据 套餐id 集合 去删除套餐")
    @DeleteMapping
    //缓存中数据 的key 是由categoryId 组成，无法通过 套餐id精确删除
    //只能全部删除
    @CacheEvict(cacheNames = "SetmealCache",allEntries = true)
    public Result delete(@RequestParam List<Long> ids){
            setmealService.delete(ids);
            return Result.success();
    }

    @ApiOperation(value = "禁用 或 启用 套餐")
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "SetmealCache",allEntries = true)
    public Result openOrdisable(@PathVariable Integer status ,Long id ){
        setmealService.openOrdisable(status,id);
        return Result.success();
    }

}
